package com.cd.javacxy.module.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cd.javacxy.common.model.entity.CxyCollection;
import com.cd.javacxy.common.model.enums.ArticleType;
import com.cd.javacxy.common.model.enums.PageSize;
import com.cd.javacxy.common.model.enums.TextLengthEnum;
import com.cd.javacxy.common.model.query.CollectionQuery;
import com.cd.javacxy.common.model.query.UpdateQuery4ArticleCount;
import com.cd.javacxy.common.utils.Page;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BaseException;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.web.dao.*;
import com.cd.javacxy.module.web.service.CxyCollectionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * CxyCollection的服务接口的实现类
 *
 * @author yanghy
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CxyCollectionServiceImpl implements CxyCollectionService {

    private final CxyCollectionDao cxyCollectionDao;
    private final CxyTopicDao cxyTopicDao;
    private final CxyKnowledgeDao cxyKnowledgeDao;
    private final CxyAskDao cxyAskDao;
    private final CxyBlogDao cxyBlogDao;

    @Override
    public PageResult<CxyCollection> findCollectionByPage(CollectionQuery collectionQuery) {
        try {
            int count = this.cxyCollectionDao.selectCount(collectionQuery);
            int pageSize = PageSize.PAGE_SIZE20.getSize();
            int pageNum = collectionQuery.getPageNum();
            Page page = new Page(pageNum, count, pageSize);
            collectionQuery.setPage(page);
            List<CxyCollection> list = this.cxyCollectionDao.selectList(collectionQuery);
            return new PageResult<>(page, list);
        } catch (Exception e) {
            log.error("获取收藏列表出错", e);
            throw new BaseException("获取收藏列表出错,请重试");
        }
    }

    @Override
    public int deleteCollection(CxyCollection collection) {
        try {
            return this.cxyCollectionDao.delete(collection);
        } catch (Exception e) {
            log.error("收藏删除出错", e);
        }
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = BussinessException.class)
    public int addCollection(CxyCollection collection) throws BussinessException {
        if (collection.getArticleId() == null || collection.getArticleType() == null
                || StrUtil.isEmpty(collection.getTitle()) ||
                collection.getTitle().length() > TextLengthEnum.TEXT_300_LENGTH.getLength()) {
            throw new BussinessException("参数错误");
        }

        CollectionQuery collectionQuery = new CollectionQuery(collection.getArticleId(), collection.getArticleType(), collection.getUserId());
        CxyCollection c = this.findCollectionByKey(collectionQuery);
        if (c != null) {
            throw new BussinessException("您已经收藏过了");
        }
        collection.setCreateTime(LocalDateTime.now());
        this.cxyCollectionDao.insert(collection);
        UpdateQuery4ArticleCount updateQuery4ArticleCount = new UpdateQuery4ArticleCount();
        updateQuery4ArticleCount.setAddCollectionCount(Boolean.TRUE);
        updateQuery4ArticleCount.setArticleId(collection.getArticleId());
        if (collection.getArticleType() == ArticleType.TOPIC) {
            this.cxyTopicDao.updateInfoCount(updateQuery4ArticleCount);
        } else if (collection.getArticleType() == ArticleType.KNOWLEDGE) {
            this.cxyKnowledgeDao.updateInfoCount(updateQuery4ArticleCount);
        } else if (collection.getArticleType() == ArticleType.Ask) {
            this.cxyAskDao.updateInfoCount(updateQuery4ArticleCount);
        } else if (collection.getArticleType() == ArticleType.BLOG) {
            this.cxyBlogDao.updateInfoCount(updateQuery4ArticleCount);
        } else {
            throw new BussinessException("参数错误");
        }
        return 0;
    }

    public CxyCollection findCollectionByKey(CollectionQuery collectionQuery) {
        List<CxyCollection> collectionList = this.cxyCollectionDao.selectList(collectionQuery);
        if (collectionList.isEmpty()) {
            return null;
        }
        return collectionList.get(0);
    }
}